/**
 * <p>Copyright:Copyright(c) 2019</p>
 * <p>Company:Professional</p>
 * <p>Package:org.cesgroup.course.web.controller</p>
 * <p>File:UserController.java</p>
 * <p>类更新历史信息</p>
 * @todo chanlong(陈龙) 创建于 Sep 2, 2019 2:09:17 PM
 */
package org.cesgroup.course.web.controller;

import org.cesgroup.course.api.bo.IUserBo;
import org.cesgroup.course.api.bo.UserBo;
import org.cesgroup.course.api.po.User;
import org.cesgroup.course.proxy.annotation.Proxy;
import org.cesgroup.course.proxy.handler.UserInvocationHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.InvocationHandler;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>描述: </p>
 * <p>Company: Professional</p>
 * @author chanlong(陈龙)
 * @date Sep 2, 2019 2:09:17 PM
 * @version 1.0.2019
 */
// 用户的Controller
@RestController @RequestMapping("/")
public class UserController {
	
	@PostMapping("user")
	// 减少了对原有代码的破坏，只需加上自定义的“@Proxy”注解即可
	@Proxy(targetMethod = "save", 
		   targetClass = UserBo.class,
		   handlerClass = UserInvocationHandler.class)
	public Object save(final User user) {
		return bo.save(user);
	}

	@PostMapping("user/proxy")
	public Object proxy(final User user) {
		InvocationHandler handler  = new UserInvocationHandler(bo);
		Enhancer hancer = new Enhancer();
        hancer.setClassLoader(UserBo.class.getClassLoader());
        hancer.setSuperclass(UserBo.class);
        hancer.setCallback(handler);
        
        UserBo proxy = (UserBo) hancer.create();
		return proxy.save(user);
	}
	
	@GetMapping("users")
	public Object users() {
		return bo.findAll();
	}
	
	@Autowired
	private IUserBo bo;
	
}
